﻿using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace WorldmeServer.Middlewares
{
    public class JsonDataTimeConverter : JsonConverter<DateTime>
    {
        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            Debug.Assert(typeToConvert == typeof(DateTime));
            return DateTime.SpecifyKind(DateTime.Parse(reader.GetString()), DateTimeKind.Local);
        }

        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }
}
